package com.A21MobileSales;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.A21.Adapter.ProductSwipeListAdapter;
import com.A21.Object.MSTProduct;
import com.A21MobileSales.DataBase.DatabaseHelper;
import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
import com.fortysevendeg.swipelistview.SwipeListView;

public class ProductMaster extends Activity {

	SwipeListView lvProduct;
	EditText edtSearchProduct;

	DatabaseHelper ObjDBHelper;

	ArrayList<MSTProduct> lstProducts;
	Button btnDelSearch;
	ProductSwipeListAdapter ProductSwipeListAdapter;

	Button btn_Home;
	ImageButton btn_back;
	String TrnType;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.products);
		try {
			ObjDBHelper = new DatabaseHelper(this);
			lvProduct = (SwipeListView) findViewById(R.id.lvProductList);
			btn_Home = (Button) findViewById(R.id.btn_home);
			btn_Home.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent _intent = new Intent();
					_intent.setClass(ProductMaster.this, HomeScreen.class);
					startActivity(_intent);
				}
			});

			btn_back = (ImageButton) findViewById(R.id.btn_back);
			btn_back.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					GoBack();
				}
			});

			btnDelSearch = (Button) findViewById(R.id.btn_del_search);
			btnDelSearch.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edtSearchProduct.setText("");
					// ProductSwipeListAdapter.notifyDataSetChanged();
					// lvProduct.setAdapter(ProductSwipeListAdapter);
				}
			});

			lvProduct.setSwipeListViewListener(new BaseSwipeListViewListener() {
				@Override
				public void onOpened(int position, boolean toRight) {
				}

				@Override
				public void onClosed(int position, boolean fromRight) {
				}

				@Override
				public void onListChanged() {
				}

				@Override
				public void onMove(int position, float x) {
				}

				@Override
				public void onStartOpen(int position, int action, boolean right) {
					// Log.d("swipe",
					// String.format("onStartOpen %d - action %d", position,
					// action));
				}

				@Override
				public void onStartClose(int position, boolean right) {
					// Log.d("swipe", String.format("onStartClose %d",
					// position));
				}

				@Override
				public void onClickFrontView(int position) {
					Intent intent = new Intent(ProductMaster.this,
							ProductInfo.class);
					Bundle bundle = new Bundle();
					bundle.putString("Code", lstProducts.get(position).Product);
					intent.putExtras(bundle);
					startActivity(intent);
					// Log.d("swipe", String.format("onClickFrontView %d",
					// position));
					// lvProduct.openAnimate(position); //when you touch front
					// view it will open
				}

				@Override
				public void onClickBackView(int position) {
					// Log.d("swipe", String.format("onClickBackView %d",
					// position));
					// lvProduct.closeAnimate(position);//when you touch back
					// view it will close
				}

				@Override
				public void onDismiss(int[] reverseSortedPositions) {

				}

			});

			// lvProduct.setSwipeMode(SwipeListView.SWIPE_MODE_BOTH); // there
			// are five swiping modes
			lvProduct.setSwipeMode(SwipeListView.SWIPE_MODE_RIGHT); // there are
																	// five
																	// swiping
																	// modes
			lvProduct.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_DISMISS); // there
																				// are
																				// four
																				// swipe
																				// actions
			lvProduct.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);
			lvProduct.setOffsetLeft(convertDpToPixel(0f)); // left side offset
			float fRight = getResources().getDimension(R.dimen.SwipeRight);
			lvProduct.setOffsetRight(convertDpToPixel(fRight)); // right side
																// offset
			lvProduct.setAnimationTime(500); // Animation time
			// lvProduct.setSwipeOpenOnLongPress(true); // enable or disable
			// SwipeOpenOnLongPress

			// lvProduct.setAdapter(ProductSwipeListAdapter);
			// ProductSwipeListAdapter.notifyDataSetChanged();

			edtSearchProduct = (EditText) findViewById(R.id.edtSearchProduct);
			edtSearchProduct.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					try {
						String text = s.toString().toLowerCase(
								Locale.getDefault());
						ProductSwipeListAdapter.filter(text);
						// lstProducts =
						// ObjDBHelper.SearchProducts(s.toString(), false);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// lvProduct.setAdapter(new ProductSwipeListAdapter(
					// ProductMaster.this, lstProducts));
				}
			});
			new ProductTask().execute();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void onBackPressed() {
		GoBack();
	}

	private void GoBack() {
		Bundle b = new Bundle();
		Intent _intent = new Intent();
		_intent.putExtras(b);
		_intent.setClass(ProductMaster.this, HomeScreen.class);
		startActivity(_intent);

	}

	public int convertDpToPixel(float dp) {
		DisplayMetrics metrics = getResources().getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return (int) px;
	}

	public void bindData() {
		ProductSwipeListAdapter = new ProductSwipeListAdapter(
				ProductMaster.this, lstProducts);
		ProductSwipeListAdapter.notifyDataSetChanged();
		lvProduct.setAdapter(ProductSwipeListAdapter);
	}

	private class ProductTask extends AsyncTask<String, Void, Void> {

		private ProgressDialog dialog = new ProgressDialog(ProductMaster.this);
		String StrMsg = null;

		protected void onPreExecute() {
			this.dialog.setMessage("Loading...");
			this.dialog.setCanceledOnTouchOutside(false);
			this.dialog.show();

		}

		protected Void doInBackground(final String... Params) {
			try {
				lstProducts = ObjDBHelper.GetAllProducts(false);
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			return null;
		}

		protected void onPostExecute(final Void unused) {
			if (this.dialog.isShowing()) {
				this.dialog.dismiss();
				this.dialog = null;
			}
			try {
				bindData();
			} catch (Exception ex) {

				ex.printStackTrace();

			}

		}

	}
}